﻿// Imports
using System.Collections.Generic;
using System.Web;

namespace Nickel
{

    /// <summary>
    /// Author
    /// </summary>
    public class Author
    {

        /// <summary>
        /// Creates an author instance
        /// </summary>
        public Author()
        { 
        }

        /// <summary>
        /// String required to activate account
        /// </summary>
        public string ActivationKey;

        /// <summary>
        /// True if user is active
        /// </summary>
        public bool Active
        {

            // User is inactive if activation key is defined
            get 
            { 
                return ActivationKey == string.Empty; 
            }

        }

        /// <summary>
        /// True if administrator
        /// </summary>
        public bool Admin;

        /// <summary>
        /// Creation date time
        /// </summary>
        public string CreationDateTime;

        /// <summary>
        /// E-mail address
        /// </summary>
        public string Email;

        /// <summary>
        /// URL to author's web site
        /// </summary>
        public string HomePageUrl;

        /// <summary>
        /// GUID
        /// </summary>
        public string ID;

        /// <summary>
        /// Date time of last login
        /// </summary>
        public string ModifiedDateTime;

        /// <summary>
        /// Display name
        /// </summary>
        public string Name;

        /// <summary>
        /// Passsword hash
        /// </summary>
        public string Password;

        /// <summary>
        /// Set and send activation key to user
        /// </summary>
        /// <param name="url">Site root URL</param>
        public void SendActivationKey(string url)
        {

            // Create activation key
            ActivationKey = StringHelper.Encode(StringHelper.Encrypt(ID));

            // Set subject
            string strSubject = "Please activate your " + Settings.SITE_NAME + " account";

            // Set body of activation e-mail
            string strBody = "Thank you for registering with " + Settings.SITE_NAME + ".  " +
                "A glorious world of comment posting awaits you. Follow the link " +
                "below to activate you account:\n\n" +
                url + ActivationKey + "\n\n" +
                "Thanks,\n" +
                Settings.SITE_OWNER;

            // Send e-mail
            Mail.Send(Email, strSubject, strBody);

        }

        /// <summary>
        /// Send activation notice user
        /// </summary>
        /// <param name="url">Site root URL</param>
        public void SendActivationNotice(string url)
        {

            // Create activation key
            ActivationKey = StringHelper.Encode(StringHelper.Encrypt(ID));

            // Set subject
            string strSubject = "Your " + Settings.SITE_NAME + " account is now activated";

            // Set body of activation e-mail
            string strBody = "Well done, " + Name + ".  Your account at " +
                Settings.SITE_NAME + " has been activated. " +
                "Get rolling by signing in here:\n\n" +
                url + "\n\n" +
                "Thanks,\n" +
                Settings.SITE_OWNER;

            // Send e-mail
            Mail.Send(Email, strSubject, strBody, true);

        }

        /// <summary>
        /// Send e-mail notifying author they are now an admin
        /// </summary>
        /// <param name="url">Site root URL</param>
        public void SendAdminNotification(string url)
        {

            // Create activation key
            ActivationKey = StringHelper.Encode(StringHelper.Encrypt(ID));

            // Set subject
            string strSubject = "Welcome to the ranks of the " + Settings.SITE_NAME + " elite";

            // Set body of activation e-mail
            string strBody = "This is just a quick message to let you know that you have " +
                "been granted administrative privileges. With great power comes great " +
                "responsibility.  Do NOT screw this up!  Log in at the link below to begin " +
                "your reign of error:\n\n" +
                url + "\n\n" +
                "Thanks,\n" +
                Settings.SITE_OWNER;

            // Send e-mail
            Mail.Send(Email, strSubject, strBody);

        }
        
        /// <summary>
        /// Send password
        /// </summary>
        /// <param name="url">Site root URL</param>
        public void SendPassword()
        {

            // Set subject
            string strSubject = Settings.SITE_NAME + " password recovery";

            // Set body of activation e-mail
            string strBody = "Your password has been decrypted using several algorithms, " +
                "went uphill both ways in the snow, and is printed below for your forgetful " +
                "eyes only:\n\n" +
                StringHelper.Decrypt(Password) + "\n\n" +
                "You may want to consider deleting this e-mail and maybe change your password " +
                "on the site now that it's been sent to you in plain text.\n\n" + 
                "Thanks,\n" +
                Settings.SITE_OWNER;

            // Send e-mail
            Mail.Send(Email, strSubject, strBody);

        }

    }

}
